package com.wenwei.sharebox.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.fragment.HomeFragment;
import com.wenwei.sharebox.fragment.PersonalFragment;
import com.wenwei.sharebox.fragment.ShareFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/6.
 */
public class HostActivity extends BaseActivity {
    @Bind(R.id.host_content)
    FrameLayout hostContent;
    @Bind(R.id.tab_home_img)
    ImageView tabHomeImg;
    @Bind(R.id.tab_home_tv)
    TextView tabHomeTv;
    @Bind(R.id.tab_share_img)
    ImageView tabShareImg;
    @Bind(R.id.tab_share_tv)
    TextView tabShareTv;
    @Bind(R.id.tab_personal_img)
    ImageView tabPersonalImg;
    @Bind(R.id.tab_personal_tv)
    TextView tabPersonalTv;

    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;
    private HomeFragment homeFragment;
    private ShareFragment shareFragment;
    private PersonalFragment personalFragment;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_host;
    }

    private String tag;
    @Override
    protected void init() {
        super.init();
        mFragmentManager = getSupportFragmentManager();
        tag = getIntent().getStringExtra("tag");
    }

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 30, null);
        if(tag!=null && tag.equals("goPersonal")){
            setTabSelection(3);
        }else {
            setTabSelection(1);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.host_home_ll, R.id.host_share_ll, R.id.host_personal_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.host_home_ll:
                setTabSelection(1);
                break;
            case R.id.host_share_ll:
                setTabSelection(2);
                break;
            case R.id.host_personal_ll:
                setTabSelection(3);
                break;
        }
    }

    private void setTabSelection(int position){
        clearSelection();
        mTransaction = mFragmentManager.beginTransaction();
        hideFragment(mTransaction);
        switch(position){
            case 1:
                tabHomeImg.setImageResource(R.drawable.icon_tab_home_select);
                tabHomeTv.setTextColor(Color.parseColor("#40CCCB"));
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    mTransaction.add(R.id.host_content, homeFragment);
                } else {
                    mTransaction.show(homeFragment);
                }
                break;
            case 2:
                tabShareImg.setImageResource(R.drawable.icon_tab_share_select);
                tabShareTv.setTextColor(Color.parseColor("#40CCCB"));
                if (shareFragment == null) {
                    shareFragment = new ShareFragment();
                    mTransaction.add(R.id.host_content, shareFragment);
                } else {
                    mTransaction.show(shareFragment);
                }
                break;
            case 3:
                tabPersonalImg.setImageResource(R.drawable.icon_tab_personal_select);
                tabPersonalTv.setTextColor(Color.parseColor("#40CCCB"));
                if (personalFragment == null) {
                    personalFragment = new PersonalFragment();
                    mTransaction.add(R.id.host_content, personalFragment);
                } else {
                    mTransaction.show(personalFragment);
                }
                break;
        }
        mTransaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (shareFragment != null) {
            transaction.hide(shareFragment);
        }
        if (personalFragment != null) {
            transaction.hide(personalFragment);
        }

    }

    private void clearSelection() {
        tabHomeImg.setImageResource(R.drawable.icon_tab_home_normal);
        tabHomeTv.setTextColor(Color.parseColor("#AEAEAE"));
        tabShareImg.setImageResource(R.drawable.icon_tab_share_normal);
        tabShareTv.setTextColor(Color.parseColor("#AEAEAE"));
        tabPersonalImg.setImageResource(R.drawable.icon_tab_personal_normal);
        tabPersonalTv.setTextColor(Color.parseColor("#AEAEAE"));
    }


}
